package com.jksoft.darewayyyy.fragment;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.jksoft.darewayyyy.R;
import com.jksoft.darewayyyy.adapter.EmployeeAdapter;
import com.jksoft.darewayyyy.pojo.Employee;
import com.jksoft.darewayyyy.util.FastBlur;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link QueueFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class QueueFragment extends Fragment implements EmployeeAdapter.RecyclerViewOnItemClickListener{
    private RecyclerView recyclerView;
    private List<Employee> employeeList;
    private EmployeeAdapter adapter;
    private LinearLayout main;
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment QueueFragment.
     */
    public static QueueFragment newInstance() {
        QueueFragment fragment = new QueueFragment();
        return fragment;
    }

    public QueueFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_queue, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.content);
        main = (LinearLayout) view.findViewById(R.id.main);
//        applyBlur();
        return view;
    }

    private void applyBlur() {
        main.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                main.getViewTreeObserver().removeOnPreDrawListener(this);
                main.buildDrawingCache();

                Bitmap bm = main.getDrawingCache();
                blur(bm, main);
                return true;
            }
        });
    }

    private void blur(Bitmap bm, View view) {
        float scaleFactor = 8;
        float radius = 2;
        Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth() / scaleFactor),
                (int) (view.getMeasuredHeight() / scaleFactor), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        canvas.translate(-view.getLeft() / scaleFactor, -view.getTop() / scaleFactor);
        canvas.scale(1 / scaleFactor, 1 / scaleFactor);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(bm, 0, 0, paint);

        overlay = FastBlur.doBlur(overlay, (int) radius, true);
        view.setBackground(new BitmapDrawable(getResources(), overlay));
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();
    }

    private void initData() {
        employeeList = new ArrayList<>();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 5);
        recyclerView.setHasFixedSize(true);
        adapter = new EmployeeAdapter(getActivity(), employeeList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter.setRecyclerViewOnItemClickListener(this);
    }

    public void addEmployee(Employee employee) {
        adapter.addItem(employee);
        recyclerView.scrollToPosition(0);
    }

    @Override
    public void onItemClickListener(View v, Employee employee) {
        Toast.makeText(getActivity(),"姓名=" + employee.getEmpName(), Toast.LENGTH_SHORT).show();
    }
}
